package com.encryption.feature.user.ui.render
{
	import com.encryption.controls.loading.DelayPopUpManager;
	import com.encryption.feature.user.ui.UserAuditEvent;
	import com.encryption.feature.user.ui.UserAuditWindow;
	import com.encryption.rpc.RPCEvent;
	import com.encryption.rpc.facade.RPCFacadeHelper;
	import com.encryption.rpc.facade.UserFacade;
	import com.encryption.rpc.model.User;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.DataGrid;
	import mx.core.FlexGlobals;
	import mx.core.UIComponent;
	import mx.managers.PopUpManager;
	import mx.rpc.events.ResultEvent;
	
	public class UserAuditOperationLabel extends UserOperationLabel
	{
		private var _userAuditPanel:UserAuditWindow;
		
		public function UserAuditOperationLabel()
		{
			super();
			
			text = "审核";
		}
		
		
		override public function set data(value:Object):void
		{
			super.user = value as User;
			
			if(user.aduitState == 1){
				this.visible = false;
				this.includeInLayout = false;
			}else{
				this.visible = true;
				this.includeInLayout = true;
			}
				
		}
		
		override protected function mouseClickHandle(event:MouseEvent):void
		{
			if(_userAuditPanel == null){
				_userAuditPanel = new UserAuditWindow();
				_userAuditPanel.addEventListener("confirm", userAuditConfirm);
			}
			PopUpManager.addPopUp(_userAuditPanel, FlexGlobals.topLevelApplication as UIComponent);
			PopUpManager.centerPopUp(_userAuditPanel);
		}
		
		private function userAuditConfirm(event:Event):void
		{
			user.aduitState = 1;
			user.fileLevel = _userAuditPanel.fileLevel;
			DelayPopUpManager.popUp("用户审核中...", FlexGlobals.topLevelApplication as UIComponent);
			new UserFacade(UserFacade.OPERATION_auditUser, {arg0:user}, userAduitHandle);
		}
		
		private function userAduitHandle(event:RPCEvent):void
		{
			RPCFacadeHelper.rpcHandle(event, function(rsEvent:ResultEvent):void{
				DelayPopUpManager.removeAllTips();
				var comp:DisplayObjectContainer = parent;
				while(!(comp is DataGrid)){
					comp = comp.parent;
				}
				
				comp.dispatchEvent(new UserAuditEvent(UserAuditEvent.USER_UPDATE, user));
			}, "审核失败，请确认数据库连接是否正常", function():void{
				DelayPopUpManager.removeAllTips();
			});
		}
	}
}